package com.wd.live.view.Activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.wd.live.R
import com.wd.live.base.BaseActivity
import com.wd.live.databinding.ActivityLoginBinding
import com.wd.live.viewmodel.LoginViewModel

class LoginActivity : BaseActivity<LoginViewModel,ActivityLoginBinding>(R.layout.activity_login) {
    private val APP_ID = "wx064904aa8809f5aa"

    // IWXAPI 是第三方app和微信通信的openApi接口
    private lateinit var api: IWXAPI
    override fun dataObserver() {

    }

    override fun initViews() {
        binding.deng.setOnClickListener {
                startActivity(Intent(this, DengActivity::class.java))
        }
        binding.login.setOnClickListener {
            api = WXAPIFactory.createWXAPI(this, APP_ID, true)

            // 将应用的appId注册到微信82

            api.registerApp(APP_ID)

            //建议动态监听微信启动广播进行注册到微信
            registerReceiver(object : BroadcastReceiver() {
                override fun onReceive(context: Context, intent: Intent) {
                    // 将该app注册到微信
                    api.registerApp(APP_ID)
                }
            }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP))
            // 发送授权消息给微信
            //吐司111
            val req = SendAuth.Req()
            req.scope = "snsapi_userinfo" // 只能填 snsapi_userinfo
            req.state = "wechat_sdk_demo_test"
            api.sendReq(req)
            finish()
        }
    }

}